﻿Imports lbGlobalcom
Public Class fClases
    Dim daClase As dsProductosTableAdapters.PRO_CLASETableAdapter
    Dim daClave As dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
    Private Sub btnCargarClases_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarClases.Click
        Try
            If Me.DsProductos.PRO_CLASE.GetChanges() IsNot Nothing Then
                PRO_CLASEBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    PRO_CLASEBindingNavigatorSaveItem.PerformClick()
                End If
            End If
            daClase.Fill(DsProductos.PRO_CLASE)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fClases_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Me.DsProductos.PRO_CLASE.GetChanges() IsNot Nothing Then
                PRO_CLASEBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    PRO_CLASEBindingNavigatorSaveItem.PerformClick()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fClases_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daClave = New dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
            daClase = New dsProductosTableAdapters.PRO_CLASETableAdapter
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub PRO_CLASEDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles PRO_CLASEDataGridView.CellBeginEdit
        Try
            If e.ColumnIndex = 1 And PRO_CLASEDataGridView.Rows(e.RowIndex).Cells(0).Value Is DBNull.Value Then
                Dim dt As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASDataTable
                dt = daClave.GetDataByIdClavePrimaria("PRO_CLASE")
                If dt.Count Then
                    Dim dr As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASRow
                    dr = dt.Rows(0)
                    Dim Cons As lbGlobalcom.ConsecTablas = New lbGlobalcom.ConsecTablas(dr("TABLA"), dr("CAMPO"), dr("LONGITUD"))
                    PRO_CLASEDataGridView.Rows(e.RowIndex).Cells(0).Value = Cons.NumeroConsec
                    Cons = Nothing
                    PRO_CLASEBindingSource.EndEdit()
                    daClase.Update(DsProductos.PRO_CLASE)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub PRO_CLASEDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles PRO_CLASEDataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub

    Private Sub PRO_CLASEBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRO_CLASEBindingNavigatorSaveItem.Click
        Try
            Validate()
            PRO_CLASEBindingSource.EndEdit()
            daClase.Update(DsProductos.PRO_CLASE)
            MessageBox.Show("Se salvarón registros con exito", "Salva Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub PRO_CLASEDataGridView_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles PRO_CLASEDataGridView.UserDeletedRow
        Try
            daClase.Update(DsProductos.PRO_CLASE)
            MessageBox.Show("Registro borrado con exito", "Elimina registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub PRO_CLASEDataGridView_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles PRO_CLASEDataGridView.UserDeletingRow
        If MessageBox.Show("Desea borrar el registro", "Elimina registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        If MessageBox.Show("Desea borrar el registro", "Elimina registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            PRO_CLASEBindingSource.Remove(PRO_CLASEBindingSource.Current)
            PRO_CLASEBindingNavigatorSaveItem.PerformClick()
        End If
    End Sub
End Class